//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.rabbitmq.common;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.config.WebSocketConfig;
import com.dict.comment.WebSocketDTO;
import com.entry.DeviceWarning;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.config.MinioConfig;
import com.config.RabbitMQConfig;
import com.dict.comment.FileDto;
import com.dict.comment.domain.entity.EventWaring;
import com.rabbitmq.api.MessageBean;
import com.service.EventWaringService;
import com.utils.JsonExample;
import org.apache.ibatis.javassist.tools.web.Webserver;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;
@Transactional
@Service
public class RabbitMQConsumer {
    private static final Logger log = LoggerFactory.getLogger(RabbitMQConsumer.class);
    @Autowired
    private EventWaringService eventWaringService;
    @Autowired
    private MinioConfig minioConfig;

    @Autowired
    private WebSocketConfig webSocketConfig;

    public RabbitMQConsumer() {
    }
    @RabbitListener(queues = "${rabbit.queueName}")
   /* @RabbitListener(queues = "${spring.rabbitmq.listener.simple.queues}")*/
    public void getMessage(String message) {
        log.info("接收到的信息:{}", message);
        DeviceWarning deviceWarning= JSON.parseObject(message,DeviceWarning.class);
        if(deviceWarning.getImgbase64()!=null){
            EventWaring eventWaring=new EventWaring();
            eventWaring.setEventTypeId(deviceWarning.getEventTypeId().toString());
//        eventWaring.setPersonImageId(Integer.valueOf(deviceWarning.getPersonImageId().toString()));
            eventWaring.setImgbase64(this.getImage(deviceWarning.getImgbase64()));
//        eventWaring.setImgbase64("this.getImage(deviceWarning.getImgbase64())");
            eventWaring.setChannelId(deviceWarning.getChannelId());
            eventWaring.setDeviceId(deviceWarning.getDeviceId());
            eventWaring.setVideo_url(deviceWarning.getVideo_url());
//        eventWaring.setRect(deviceWarning.getDetRegions().get(0).getObjects().get(0).getRect().toString());
//        eventWaring.setScore(Double.parseDouble(deviceWarning.getDetRegions().get(0).getObjects().get(0).getScore()));
            eventWaring.setTime(deviceWarning.getTime().toString());
            log.info("要保存的数据:{}", eventWaring);

            Integer integer = eventWaringService.saveEventWaring(eventWaring);
            log.info("要保存的结果***************:{}", integer);
            WebSocketDTO webSocketDTO=new WebSocketDTO();
            webSocketDTO.setDestination(webSocketConfig.getWsUrl());
            webSocketDTO.setPayload(JSONUtil.toJsonStr(eventWaring));
            String post = HttpUtil.post(webSocketConfig.getWebSocketUrl(), JSONUtil.toJsonStr(webSocketDTO));
            log.info("要webSocket请求结果***************:{}",post);
        }

    }

    public  String getImage(String base64){
        FileDto fileDto = new FileDto();
        fileDto.setFileName(JsonExample.getNowDate());
        fileDto.setBucketName(minioConfig.getBucketName());
        fileDto.setFileType("jpg");
        //获取base64数据
        fileDto.setBase64(JsonExample.extractBase64Data(base64));
        //向minio发请求存储
        String post = HttpUtil.post(minioConfig.getUploadurl(), JSONUtil.toJsonStr(fileDto));
        //获取存储路径
        // 解析 JSON 字符串为 JsonObject
        JsonObject jsonObject = JsonParser.parseString(post).getAsJsonObject();
        // 提取 data 字段的值
        String filepath = jsonObject.get("data").getAsString();
        //拼接路径
        String realPath = minioConfig.getGetimageurl()+filepath;
        return realPath;
    }
}
